const { json } = require('express')
const fs = require('fs')
const path = require('path')

const filePath = path.join(__dirname, './data.json')
function read() {
  return new Promise(function (resolve, reject) {
    fs.readFile(filePath, 'utf8', (err, data) => {
      if (err) reject(err)
      resolve(JSON.parse(data))
    })
  })
}

function write(data) {
  return new Promise(function (resolve, reject) {
    fs.writeFile(filePath, JSON.stringify(data), (err) => {
      if (err) reject(err)
      resolve()
    })
  })
}

module.exports = {
  read,
  write
}